package ArraySecond;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Array9 {
    /**
     * 给你一个大小为 m x n 的矩阵 mat ，请以对角线遍历的顺序，用一个数组返回这个矩阵中的所有元素。
     * <p>
     * <p>
     * 示例 1：
     * <p>
     * <p>
     * 输入：mat =  [[1,2,3],
     * [4,5,6],
     * [7,8,9]]
     * 输出：[1,2,4,7,5,3,6,8,9]
     * <p>
     *
     * @param mat
     * @return
     */
    public static int[] findDiagonalOrder(int[][] mat) {
        int row = mat.length;
        int col = mat[0].length;
        int[] result = new int[row * col];
        int m = 0;
        List<Integer> tempList = new ArrayList<>();
        for (int i = 0; i < row + col - 1; i++) {
            tempList.clear();
            int r = i < col ? 0 : i - col + 1;
            int c = i < col ? i : col - 1;

            while (r < row && c > -1) {
                tempList.add(mat[r][c]);
                r++;
                c--;
            }
            if (i % 2 == 0) {
                Collections.reverse(tempList);
            }
            for (Integer integer : tempList) {
                result[m++] = integer;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[] diagonalOrder = findDiagonalOrder(matrix);
        for (int i = 0; i < diagonalOrder.length; i++) {
            System.out.println(diagonalOrder[i]);
        }
//        for (int[] ints : matrix) {
//            for (int i = 0; i < ints.length; i++) {
//                System.out.print(ints[i]);
//            }
//            System.out.println();
//        }
        System.out.println("cpu"+Runtime.getRuntime().availableProcessors());
    }
}
